<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{User, Apply};
use app\common\validate\{UserValidate, ApplyValidate};

class UserController extends BaseController
{
    private $userModel;
    public function __construct(User $userModel)
    {
        parent::initialize();
        $this->userModel = $userModel;
    }

    /**
     * 渲染用户列表页面
     */
    public function user_list()
    {
        return view();
    }

    /**
     * 获取用户数据列表
     */
    public function user_data()
    {
        $data = input('get.');

        try{
            validate(UserValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        if($data && isset($data['key'])){
            if(!empty($data['key']['mobile'])){
                $where[] = ['mobile', 'like', '%' . $data['key']['mobile'] . '%'];
            }

            if(!empty($data['key']['nickname'])){
                $where[] = ['nickname', 'like', '%' . $data['key']['nickname'] . '%'];
            }
        }

        /** 查询数据总条数 */
        $count = $this->userModel->getNum($where)['data'];

        $list = $this->userModel->getList($where, 1, $data['page'], $data['limit']);

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }




    /** 渲染代理列表页面 */
    public function agent_list()
    {
        return view();
    }

    /** 代理数据列表 */
    public function agent_data()
    {
        $data = input('get.');

        try{
            validate(ApplyValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel  = new User();
        $applyModel = new Apply();

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname'])){
                $userIds = $userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                
                $where[] = [['user_id', 'in', $userIds]];
            }

            if(!empty($data['key']['real_name'])){
                $where[] = [['real_name', 'like', '%' . $data['key']['real_name'] . '%']];
            }
        }

        /** 查询数据总条数 */
        $count = $applyModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $applyModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as &$v){
            $v['nickname'] = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /**
     * 通过操作
     */
    public function agent_pass()
    {   
        $data = input('');

        try{
            validate(ApplyValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $applyModel = new Apply();

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $applyModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }

    /**
     * 驳回
     */
    public function agent_back()
    {   
        $data = input('');

        try{
            validate(ApplyValidate::class)->scene('back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $applyModel = new Apply();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $applyModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }
}
